import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import store from '@/store'
// import iView from 'iview'
import iView from 'view-design'
import { setToken, getToken, canTurnTo, setTitle,setHasLogin,setUserId,setAccess,getParams,getHlmsAuthorizationPage } from '@/libs/util'
import config from '@/config'

const { homeName } = config

Vue.use(Router)
const router = new Router({
	routes,
	// mode: 'history'
})
const LOGIN_PAGE_NAME = 'login'
const AUTHORIZATION_PAGE_NAME = 'authorization'
const turnTo = (to, access, next) => {
	if (canTurnTo(to.name, access, routes)) next() // 有权限，可访问
	else next({ replace: true, name: 'error_401' }) // 无权限，重定向到401页面
}

router.beforeEach((to, from, next) => {
	iView.LoadingBar.start()
	next()
	// TODO请求拦截
  	// let token = getToken()
	// if ((!token || token==undefined) && to.name !== LOGIN_PAGE_NAME && to.name !== AUTHORIZATION_PAGE_NAME) {
	// 	let destination = {name:LOGIN_PAGE_NAME};
	// 	if (to.query.sid != undefined) {
	// 		destination.name = AUTHORIZATION_PAGE_NAME;
	// 		destination.query = {sid:to.query.sid,toName:to.name}
	// 	}
	// 	next(destination)
		
	// } else if ((!token || token==undefined) && (to.name === LOGIN_PAGE_NAME || to.name === AUTHORIZATION_PAGE_NAME)) {
	// 	// 未登陆且要跳转的页面是登录页
	// 	next() // 跳转
	// } else if ( token && token!=undefined && (to.name === LOGIN_PAGE_NAME || to.name === AUTHORIZATION_PAGE_NAME) ) {
    //     // 已登录且要跳转的页面是登录页
	// 	next({
	// 		name: homeName // 跳转到homeName页
	// 	})
		
	// } else {
	// 	console.log(4);
	// 	if (store.state.user.hasLogin) {
    //     	turnTo(to, store.state.user.access, next)
	// 	} else {
    //         setToken('')
    //         setHasLogin('');
    //         setUserId('');
    //         setAccess('');
	// 		next({
	// 			name: LOGIN_PAGE_NAME
	// 		})
	// 	}
	// }
})

router.afterEach(to => {
	setTitle(to, router.app)
	iView.LoadingBar.finish()
	window.scrollTo(0, 0)
})

export default router;
